﻿import QtQuick 2.8
import QtQuick.Controls 1.4
import QtGraphicalEffects 1.0


import "../../"
import "../../Global"
//import "Temp.js" as PokeClass

GameItem {
    //property int type   //
    property bool bRectangularGlow: false   //是否显示点击后发光边框
    property bool bTemp: false              //是临时牌,则bEnable和bClicked和flags无效(bClicked自动设置)
    property bool bZhengMian : true         //: (flags & PokeClass.flag_ZhengMian)
    //property int iZBackup                   //z序备份,目前没用


    property alias pokeSource : image.source
    property alias pokeBackSource : imageBack.source

    property alias sourceSize: image.sourceSize
    property alias mouseArea: mouseArea



    signal debugMessage(string str)
/*
    onBClickedChanged: {
        console.debug("谁tmd改我:",bClicked)
    }*/

    id: root
    //enabled: false
    //visible: flags & PokeClass.flag_Show
    objectName: "pokeObject"
    transformOrigin: Item.Center
    bEnable: (flags & PokeClass.Flag_Enable)           //是否可点
    bClicked: (flags & PokeClass.Flag_Clicked)          //点击状态
    width: image.width
    height: image.height



//这里是边框效果
    RectangularGlow {
        id: rectGlow
        visible: bRectangularGlow && root.bEnable && (root.bClicked)// || mouseArea.containsMouse)
        anchors.fill: rect;
        glowRadius: 7  //边框宽度
        spread: 0.4;    //虚化程度(0~1)
        color: root.bClicked ? "lightgreen" : "blue"  //颜色
        //cornerRadius: rect.radius + glowRadius
    }

//牌的矩形
    Item {
        id: rect
        width: root.width
        height: root.height
        //color: "red"

        //property alias buttonText: textItem.text	//


        //图片
        Image {
            id: image
            width: parent.width
            height: parent.height
            //source: "Pai101.jpg"
            //anchors.fill: parent

        }

        Image {
            id: imageBack
            width: parent.width
            height: parent.height
            visible: false
            //source: "Pai101.jpg"
            //anchors.fill: parent

        }

        //阴影
        DropShadow {
            visible: false
            anchors.fill: image; source: image
            horizontalOffset: 4; verticalOffset: 4
            radius: 12.0; samples: 16; color: "#80000000";
        }


        //鼠标事件
        MouseArea {
            id: mouseArea
            //enabled: root.bEnable
            hoverEnabled: true
            anchors.fill: image
            acceptedButtons: Qt.LeftButton | Qt.RightButton
            //propagateComposedEvents: true //当为true时，可以传递鼠标事件。默认值为false

            Timer { //鼠标放在上面的时间
                id: timer
                interval: 1000
                repeat: false
                running: false
                triggeredOnStart: false
                onTriggered: {
                    s_holdTriggered(userData)
                }
            }
            onEntered: if(bEnable)timer.start()
            onExited: timer.stop()

            onClicked: {
                //console.log(mouse.x);
                //console.log(width);
                if ( mouse.button == Qt.LeftButton ) {
                    if(bEnable) {
                        //flags ^= PokeClass.flag_Clicked
                        root.clicked(bClicked,userData);
                        //console.debug(bClicked)
                        if(bTemp) {
                            bClicked = !bClicked
                            //bClicked = true;
                        }
                        //root.scale = 1;
                        //console.debug("qml中点击是:",userData)
                    }
                    else {
                        mouse.accepted = true
                    }
                }
                else {
                    console.debug("userData:",userData,",",flags,",",root.z)
                    debugMessage("poke data:" + userData + "," + flags + "," + root.z)
                }

                //console.debug("onClicked")
            }
            //onEntered: if(bEnable){root.scale = 1.03; root.z += 1;}
            //onExited: if(bEnable){root.scale = 1; root.z -= 1;}
            onDoubleClicked: {
                console.debug("userData:",userData,",",flags,",",root.z)
                debugMessage("poke data:" + userData + "," + flags + "," + root.z)
            }
            //onPressAndHold: console.debug("onPressAndHold")
            //onPressed: console.debug("onPressed")
            //onReleased: console.debug("onReleased")
        }


/*
        Glow {
            anchors.fill: image; source: image
            radius: 16; samples: 24
            color: "red"; spread: 0.01
            fast: true;
        }
        */


    }

    //颜色遮罩(不可用时黑色,source必须和本对象同一层次!!!)
    ColorOverlay {
        id: colorOverlay
        visible: !root.bEnable
        anchors.fill: rect;
        source: rect;
        color: "#80000000"
    }

    Component.onCompleted: {
    }

    onEnabledChanged: {

    }

    QtObject {
        id: _private

        function changeSource() {
            if(bZhengMian) {
                image.visible = true;
                imageBack.visible = false;
            }
            else {
                image.visible = false;
                imageBack.visible = true;
            }

        }
    }


    onBZhengMianChanged: {
        _private.changeSource();
    }
    //onPokeSourceChanged: _private.changeSource()
    //onPokeBackSourceChanged: _private.changeSource()
}

